package com.example.firstapp;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.icu.util.Calendar;
import android.os.Bundle;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.HashSet;
import java.util.Set;

public class Test01Activity extends AppCompatActivity {
    Button bt_NormalDialog, bt_SingleDialog, bt_multiDialog, bt_progressDialog_level,
            bt_progressDialog_circle, bt_dateDialog, bt_timeDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test01);

        //绑定组件
        bt_NormalDialog = findViewById(R.id.bt_NormalDialog);
        bt_SingleDialog = findViewById(R.id.bt_SingleDialog);
        bt_multiDialog = findViewById(R.id.bt_multiDialog);
        bt_progressDialog_level = findViewById(R.id.bt_progressDialog_level);
        bt_progressDialog_circle = findViewById(R.id.bt_progressDialog_circle);
        bt_dateDialog = findViewById(R.id.bt_dateDialog);
        bt_timeDialog = findViewById(R.id.bt_timeDialog);



        /*
         * 监听组件
         * 用匿名监听组件
         */
        //普通对话框按钮监听
        bt_NormalDialog.setOnClickListener(view -> {
            AlertDialog dialog;
            dialog = new AlertDialog.Builder(this).setTitle("普通对话框")
                    .setMessage("是否确定退出？")
                    .setIcon(R.mipmap.ic_ke)
                    .setPositiveButton("确定", null)
                    .setNegativeButton("取消", null)
                    .create();
            dialog.show();
        });

        //单选对话框按钮监听
        bt_SingleDialog.setOnClickListener(view -> {
            final int[] k = {0};//默认选择
            AlertDialog dialog;
            dialog = new AlertDialog.Builder(this).setTitle("请选择性别")
                    .setIcon(R.mipmap.ic_ke)
                    .setSingleChoiceItems(new String[]{"男", "女"}, k[0],
                            new DialogInterface.OnClickListener() {
                                //监听选择项，修改k[0]的值
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    if (i == 0)
                                        k[0] = 0;
                                    else
                                        k[0] = 1;
                                }
                            })
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        //监听确定按钮，根据k[0]的值弹出消息提示
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            if (k[0] == 0)
                                Toast.makeText(Test01Activity.this, "男被选中", Toast.LENGTH_SHORT).show();
                            else
                                Toast.makeText(Test01Activity.this, "女被选中", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .create();
            dialog.show();
        });

        //多选对话框按钮监听
        bt_multiDialog.setOnClickListener(view -> {
            AlertDialog dialog;
            Set<String> set = new HashSet<>();
            String[] itm = new String[]{"旅游", "汽车", "美食", "宠物"};
            dialog = new AlertDialog.Builder(this).setTitle("多选对话框")
                    .setMultiChoiceItems(itm, null,
                            new DialogInterface.OnMultiChoiceClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                                    if (b)
                                        set.add(itm[i]);
                                    else set.remove(itm[i]);
                                }
                            })
                    .setIcon(R.mipmap.ic_ke)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(Test01Activity.this, set.toString(), Toast.LENGTH_SHORT).show();

                        }
                    })
                    .create();
            dialog.show();
        });

        //水平进度条对话框按钮监听
        bt_progressDialog_level.setOnClickListener(view -> {
            ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.setTitle("进度条对话框");
            progressDialog.setIcon(R.mipmap.ic_ke);
            progressDialog.setMessage("下载中...");
            //设置样式(圆形STYLE_SPINNER)
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.show();
            Thread t = new Thread(() -> {
                try {
                    for (int i = 0; i <= 100; i++) {
                        Thread.sleep(60);
                        progressDialog.setProgress(i);
                    }
                    Thread.sleep(100);
                    progressDialog.cancel();//删除窗户
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            t.start();
        });

        //圆形进度条对话框按钮监听
        bt_progressDialog_circle.setOnClickListener(view -> {
            ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.setTitle("进度条对话框");
            progressDialog.setIcon(R.mipmap.ic_ke);
            progressDialog.setMessage("下载中...");
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.show();
            Thread t = new Thread(() -> {
                try {
                    for (int i = 0; i <= 100; i++) {
                        Thread.sleep(60);
                        progressDialog.setProgress(i);
                    }
                    Thread.sleep(100);
                    progressDialog.cancel();//删除窗户
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            t.start();
        });

        //日期选择对话框按钮监听
        bt_dateDialog.setOnClickListener(view -> {
            //获取当前日期
            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int monthOfYear = calendar.get(Calendar.MONTH);
            int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
            DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                    new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker arg0, int year, int monthOfYear, int dayOfMonth) {
                            // 把获取的日期显示在文本框内，月份从0开始计数，所以要加1
                            String date = "已选中：" + year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;
                            Toast.makeText(Test01Activity.this, date,
                                    Toast.LENGTH_SHORT).show();
                        }
                    }, year, monthOfYear, dayOfMonth); //设置初始位置
            datePickerDialog.show();
        });

        //时间选择对话框按钮监听
        bt_timeDialog.setOnClickListener(view -> {
            //获取当前时间
            Calendar calendar = Calendar.getInstance();
            int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
            int minute = calendar.get(Calendar.MINUTE);
            TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                    new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                            String time = "已选中：" + hourOfDay + ":" + minute;
                            Toast.makeText(Test01Activity.this, time,
                                    Toast.LENGTH_SHORT).show();
                        }
                    }, hourOfDay, minute, true); // 最后一个参数设置是否为24小时制
            timePickerDialog.show();
        });
    }
}